﻿using System;
using System.Web.UI.WebControls;
using BONSAI.BLL;
using BONSAI.Common.DTO;
using BONSAI.Common;
namespace BONSAI.Admin.UC
{
    public partial class TypeProductsManager : System.Web.UI.UserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindStart();
            }
        }
        public void BindStart()
        {
            //// delete all
            //if (Request.QueryString["zxc"] != null)
            //{
            //    if (Request.QueryString["zxc"].ToString() == "1qwertyuiop")
            //    {
            //        em.Trainee_Delete(0);
            //    }
            //}
            ////=============

            var table = (new TypeProductsBO()).GetAll();
            GridView1.DataSource = table;
            GridView1.DataBind();

            Application[BONSAIConstants.LISTALLTYPE] = (new TypeProductsBO()).GetAll();
        }
      
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindStart();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //string key = GridView1.DataKeys[e.RowIndex].Value.ToString();
            //ad.Advisory_Delete(Int32.Parse(key),0);
            //BindStart();    
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindStart();
        }

        protected void btcapnhat_Click(object sender, EventArgs e)
        {
            if (txtloaiprodcutsShort.Text == "")
            {
                WebMBox.Show("Vui lòng nhập thông tin: Tên loại tours.");
                return;
            }
            if (txtvitri.Text == "")
            {
                WebMBox.Show("Vui lòng nhập vị trí.");
                return;
            }

            #region cập nhật

            var typeDto = new TypeProductsDTO
                              {
                                  NameSort = txtloaiprodcutsShort.Text,
                                  NameFull = txtloaiprodcutsLong.Text,
                                  Postion = Int32.Parse(txtvitri.Text),
                                  Createdate = DateTime.Now
                              };

            (new TypeProductsBO()).Insert(typeDto);
            WebMBox.Show("Thêm mới thành công.");

            #endregion

            BindStart();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindStart();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var dataKey = GridView1.DataKeys[e.RowIndex];
            if (dataKey != null)
            {
                string key = dataKey.Value.ToString();            
                string tenNgan = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
                string tenLong = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
                string viTri = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;

                if (viTri == "")
                {
                    WebMBox.Show("Vui lòng nhập : Vị Trí");
                    return;
                }

                var typeDto = new TypeProductsDTO
                                  {
                                      TypeProductsID = key,
                                      NameSort = tenNgan,
                                      NameFull = tenLong,
                                      Postion = Int32.Parse(viTri),
                                      Createdate = DateTime.Now
                                  };

                (new TypeProductsBO()).UpdateByID(typeDto);
            }
            GridView1.EditIndex = -1;
            BindStart();

        }
    }
}